﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XianboSite.Models;
using NHibernate;
using XianboSite.Common;

namespace XianboSite.Controllers
{
    public class LiveDetailsInfoController : Controller
    {
        //
        // GET: /LiveDetailsInfo/

        public ActionResult Index(string id)
        {
            ViewData["categories"] = DataAccess.GetLiveCategories();
            ViewData["livedetails"] = DataAccess.GetLiveDetails(id, 0, 10);
            ViewData["live"] = DataAccess.GetLive(id);
            ViewData["url"] = HttpContext.Request.UrlReferrer;
            return View();
        }
        
        public ActionResult LiveDetailtsInfo()
        {
            ISession session = XianboSite.Utility.NHibernateHelper.GetCurrentSession();
            string hql = "from User where 1=1";
            //string hql = "from User where id=:id";
            IQuery query = session.CreateQuery(hql);
            //query.SetParameter("", 1);
            query.SetFirstResult(0);
            query.SetMaxResults(10);
            IList<User> list = query.List<User>();

            ViewData["livedetails"] = list;
            return PartialView("LiveDetailsInfoControl");
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateDetails(FormCollection collection)
        {
            string livedetails = collection["content"];
            string liveid = collection["hliveid"];
            DataAccess.CreateLiveDetails(new LiveDetails()
            {
                DetailsID = RndIDHelper.GetRnd(10, true, true, true, false, string.Empty),
                DetailsInfo = livedetails,
                PublishTime = DateTime.Now,
                LiveID = liveid
            });
            return Redirect("/Home/Index");
        }

        public ContentResult GetLiveDetailsByXML(FormCollection collection)
        {
            DateTime lastTime = Convert.ToDateTime(collection["lasttime"]);
            string liveid = collection["liveid"];
            IList<LiveDetails> list = DataAccess.GetLiveDetails(lastTime, liveid);
            LiveDetails[] ds = list.ToArray<LiveDetails>();
            string str = string.Empty;
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
            {
                System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(LiveDetails[]));
                ser.Serialize(stream, ds);
                str = System.Text.Encoding.UTF8.GetString(stream.ToArray());
            }

            str = str.Replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

            return Content(str, "text/xml");
        }

        public JsonResult GetLivesByJson(DateTime lastTime)
        {
            var jsonData = new
            {
                //total = "",
                //page = "",
                //records = totalRecords,

                //rows = (

                //        from order in orders
                //        select new
                //        {
                //            cell = new[] { order.ID.ToString(), order.Movie_Name.ToString(), order.Realease_Date.ToString() }
                //        }).ToArray()

            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

    }
}
